home *** CD-ROM | disk | FTP | other *** search
/ Aminet 25 / Aminet 25 (1998)(GTI - Schatztruhe)[!][Jun 1998].iso / Aminet / util / pack / xpk_Source.lha / xpk_Source / libraries / DUKE / xpkDUKE.c next >
C/C++ Source or Header  |  1998-02-08  |  6KB  |  262 lines

  1. /*
  2.  * This library is mainly intended to demonstrate how to program a sub
  3.  * library.
  4.  */
  5.  
  6. #define VERSION        1
  7.  
  8. #include <xpk/xpksub.h>
  9. #include <exec/memory.h>
  10. #include <proto/exec.h>
  11.  
  12. #define SDI_TO_ANSI
  13. #include "SDI_ASM_STD_protos.h"
  14.  
  15. #ifdef __MAXON__
  16.   #define __asm
  17. #endif
  18.  
  19. #define SCANBUFLEN 32768
  20.  
  21. /*
  22.  * Pack a chunk
  23.  */
  24.  
  25. struct NukeData {
  26.     APTR    inbuf;
  27.     APTR    outbuf;
  28.     APTR    last;
  29.     APTR    next;
  30.     ULONG    cwri;
  31.     ULONG    uwri;
  32.     ULONG    uend;
  33.     ULONG    delpos;
  34.     UWORD    ulen;
  35.     UWORD    clen;
  36.     UWORD    ustop;
  37.     UWORD    ustart;
  38.     UWORD    ucount;
  39.     UWORD    scanrange;
  40.     UWORD    room1;
  41.     UWORD    room2;
  42.     UWORD    room4;
  43.     UWORD    roomN;
  44.     UWORD    data1;
  45.     UWORD    data2;
  46.     ULONG    data4;
  47.     ULONG    dataN;
  48.     ULONG    dest1;
  49.     ULONG    dest2;
  50.     ULONG    dest4;
  51.     ULONG    destN;
  52.     ULONG    dummy;
  53.     ULONG    contbuf;
  54.     ULONG    flags;
  55. };
  56.  
  57. #ifdef __cplusplus
  58. extern "C" {
  59. #endif
  60.  
  61. LONG __asm AsmPack  (register __a4 struct NukeData *ND);
  62. STRPTR __asm AsmUnpack(register __a1 STRPTR wread, register __a4 STRPTR bread,
  63.                         register __a0 STRPTR dst, register __a2 STRPTR end);
  64. #ifdef __cplusplus
  65. }
  66. #endif
  67.  
  68. #define PACKMEM   (65536+SCANBUFLEN)*sizeof(UWORD)+sizeof(struct NukeData)+SCANBUFLEN
  69. #define UNPACKMEM SCANBUFLEN
  70. #define A3000     XPKMF_A3000SPEED
  71.  
  72. static struct XpkMode DukeMode = {
  73.   NULL,       // Next mode
  74.   100,        // Handles up to
  75.   A3000,      // Flags
  76.   PACKMEM,    // Packing memory
  77.   UNPACKMEM,  // Unpacking memory
  78.   36,         // Packing speed
  79.   630,        // Unpacking speed
  80.   454,        // Compression ratio
  81.   0,          // Reserved
  82.   "normal",   // Description
  83. };
  84.  
  85. static struct XpkInfo DukeInfo = {
  86.     1,               /* info version */
  87.     VERSION,         /* lib  version */
  88.     0,               /* master vers  */
  89.     1,               /* ModesVersion */
  90.     (STRPTR) "DUKE", /* short name   */
  91.     (STRPTR) "Duke", /* long name    */
  92.     (STRPTR) "NUKE with a delta preprocessor", /* Description */
  93.     0x44554B45,     /* 'DUKE', 4 letter ID  */
  94.     XPKIF_PK_CHUNK | /* flags        */
  95.     XPKIF_UP_CHUNK,
  96.     30000,           /* max in chunk */
  97.     10,              /* min in chunk */
  98.     30000,           /* def in chunk */
  99.     (STRPTR) "Duking",   /* pk message   */
  100.     (STRPTR) "Unduking", /* up message   */
  101.     (STRPTR) "Duked",    /* pk past msg  */
  102.     (STRPTR) "Unduked",  /* up past msg  */
  103.     50,              /* DefMode      */
  104.     0,               /* Pad          */
  105.     &DukeMode,       /* ModeDesc     */
  106.     0,               /* MinModeDesc  */
  107.     0,               /* MaxModeDesc  */
  108.     0,0,0,0          /* reserved     */
  109. };
  110.  
  111. void DoDelta(STRPTR dst, STRPTR src, LONG len)
  112. {
  113.   UBYTE a=0, b=0;
  114.  
  115.   if (len>0) {
  116.     switch (len & 7) {
  117.       do {
  118.     case 0: *dst++ = (UBYTE) ((b=*src++) - a);
  119.     case 7: *dst++ = (UBYTE) ((a=*src++) - b);
  120.     case 6: *dst++ = (UBYTE) ((b=*src++) - a);
  121.     case 5: *dst++ = (UBYTE) ((a=*src++) - b);
  122.     case 4: *dst++ = (UBYTE) ((b=*src++) - a);
  123.     case 3: *dst++ = (UBYTE) ((a=*src++) - b);
  124.     case 2: *dst++ = (UBYTE) ((b=*src++) - a);
  125.     case 1: *dst++ = (UBYTE) ((a=*src++) - b);
  126.       } while ((len-=8)>0);
  127.     }
  128.   }
  129. }
  130.  
  131. void UndoDelta(STRPTR dst, STRPTR src, LONG len)
  132. {
  133.   UBYTE l = 0;
  134.  
  135.   if (len > 0) {
  136.     switch (len & 7) {
  137.      do {
  138.     case 0: *dst++ = (l+=*src++);
  139.     case 7: *dst++ = (l+=*src++);
  140.     case 6: *dst++ = (l+=*src++);
  141.     case 5: *dst++ = (l+=*src++);
  142.     case 4: *dst++ = (l+=*src++);
  143.     case 3: *dst++ = (l+=*src++);
  144.     case 2: *dst++ = (l+=*src++);
  145.     case 1: *dst++ = (l+=*src++);
  146.       } while ((len-=8)>0);
  147.     }
  148.   }
  149. }
  150.  
  151. /*
  152.  * Returns an info structure about our packer
  153.  */
  154.  
  155. #ifdef __cplusplus
  156. extern "C"
  157. #endif
  158.  
  159. struct XpkInfo *XpksPackerInfo(void)
  160. {
  161.   return &DukeInfo;
  162. }
  163.  
  164. #ifdef __cplusplus
  165. extern "C"
  166. #endif
  167.  
  168. #define LASTSIZE    (65536*sizeof(UWORD))
  169. #define NEXTSIZE    (SCANBUFLEN*sizeof(UWORD))
  170.  
  171. void __asm XpksPackFree(register __a0 struct XpkSubParams *xpar)
  172. {
  173.   struct NukeData *ND = (struct NukeData *) xpar->xsp_Sub[0];
  174.  
  175.   if(xpar->xsp_Sub[1])
  176.   {
  177.     FreeMem((APTR) xpar->xsp_Sub[1], SCANBUFLEN);
  178.     xpar->xsp_Sub[1] = 0;
  179.   }
  180.   if(ND)
  181.   {
  182.     if(ND->last) FreeMem(ND->last, LASTSIZE);
  183.     if(ND->next) FreeMem(ND->next, NEXTSIZE);
  184.     FreeMem(ND, sizeof(struct NukeData));
  185.     xpar->xsp_Sub[0] = 0;
  186.   }
  187. }
  188.  
  189.  
  190. struct NukeData *alloctabs(struct XpkSubParams *xpar)
  191. {
  192.   struct NukeData *ND;
  193.  
  194.   if ((xpar->xsp_Sub[0] = (LONG) (ND = (struct NukeData*) AllocMem(sizeof(struct NukeData),MEMF_CLEAR))) &&
  195.       (ND->last = AllocMem(LASTSIZE,0)) &&
  196.       (ND->next = AllocMem(NEXTSIZE,0)) &&
  197.       (xpar->xsp_Sub[1] = (LONG) AllocMem(SCANBUFLEN,0))) {
  198.     return ND;
  199.   }
  200.   else
  201.   {
  202.     XpksPackFree(xpar);
  203.     return 0;
  204.   }
  205. }
  206.  
  207. #ifdef __cplusplus
  208. extern "C"
  209. #endif
  210.  
  211. LONG __asm XpksPackChunk(register __a0 struct XpkSubParams *xpar )
  212. {
  213.   struct NukeData *ND=(struct NukeData *)xpar->xsp_Sub[0];
  214.  
  215.   if (!ND)
  216.     if (!(ND=alloctabs(xpar)))
  217.       return XPKERR_NOMEM;
  218.  
  219.   DoDelta((STRPTR)xpar->xsp_Sub[1], (STRPTR) xpar->xsp_InBuf, xpar->xsp_InLen);
  220.  
  221.   ND->inbuf = (APTR) xpar->xsp_Sub[1];
  222.   ND->outbuf= xpar->xsp_OutBuf;
  223.   ND->ulen  = (UWORD) xpar->xsp_InLen;
  224.   ND->clen  = (UWORD) (xpar->xsp_OutBufLen-4);
  225.  
  226.   memset(ND->last,0,LASTSIZE);
  227.   memset(ND->next,0,NEXTSIZE);
  228.  
  229.   if(((LONG)(xpar->xsp_OutLen = AsmPack(ND))) <0 || xpar->xsp_OutLen>xpar->xsp_InLen)
  230.   {
  231. /*    XpksPackReset(xpar); ist empty function */
  232.     return XPKERR_EXPANSION;
  233.   }
  234.   return 0;
  235. }
  236.  
  237. #ifdef __cplusplus
  238. extern "C"
  239. #endif
  240.  
  241. LONG __asm XpksUnpackChunk(register __a0 struct XpkSubParams *xpar)
  242. {
  243.   if(!xpar->xsp_Sub[1] && !(xpar->xsp_Sub[1] = (LONG) AllocMem(SCANBUFLEN,0)))
  244.      return XPKERR_NOMEM;
  245.  
  246.   AsmUnpack((STRPTR) xpar->xsp_InBuf,  (STRPTR)xpar->xsp_InBuf  + xpar->xsp_InLen,
  247.             (STRPTR)xpar->xsp_Sub[1], (STRPTR)xpar->xsp_Sub[1] + xpar->xsp_OutLen);
  248.  
  249.   UndoDelta((STRPTR)xpar->xsp_OutBuf, (STRPTR )xpar->xsp_Sub[1], xpar->xsp_OutLen);   
  250.  
  251.   return 0;
  252. }
  253.  
  254. #ifdef __cplusplus
  255. extern "C"
  256. #endif
  257.  
  258. void __asm XpksUnpackFree(register __a0 struct XpkSubParams *xpar)
  259. {
  260.   if (xpar->xsp_Sub[1]) FreeMem((APTR)xpar->xsp_Sub[1], SCANBUFLEN), xpar->xsp_Sub[1]=0;
  261. }
  262.